package com.cheng.controller;

import com.cheng.pojo.AppCategory;
import com.cheng.pojo.AppInfo;
import com.cheng.pojo.AppVersion;
import com.cheng.pojo.DataDictionary;
import com.cheng.service.appcategory.AppCategoryService;
import com.cheng.service.appinfo.AppInfoService;
import com.cheng.service.appversion.AppVersionService;
import com.cheng.service.datadictionary.DataDictionaryService;
import com.cheng.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SuppressWarnings("all")
@RequestMapping(value = "/app")
public class AppInfoController {
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private AppVersionService appVersionService;

    @RequestMapping(value = "/queryAppinfo")
    public String queryAppinfo(HttpServletRequest request, Model model) {
        String querySoftwareName = request.getParameter("querySoftwareName");
        String queryFlatformId = request.getParameter("queryFlatformId");

        String queryCategoryLevel1 = request.getParameter("queryCategoryLevel1");
        String queryCategoryLevel2 = request.getParameter("queryCategoryLevel2");
        String queryCategoryLevel3 = request.getParameter("queryCategoryLevel3");

        String pageIndex = request.getParameter("pageIndex");

        AppInfo appInfo = new AppInfo();
        if (querySoftwareName == null) {
            appInfo.setSoftwareName("");
        } else {
            appInfo.setSoftwareName(querySoftwareName);
        }

        if (queryFlatformId != null && queryFlatformId != "") {
            appInfo.setFlatformId(Integer.parseInt(queryFlatformId));
        }

        if (queryCategoryLevel1 != null && queryCategoryLevel1 != "") {
            appInfo.setCategoryLevel1(Integer.parseInt(queryCategoryLevel1));
        }

        if (queryCategoryLevel2 != null && queryCategoryLevel2 != "") {
            appInfo.setCategoryLevel2(Integer.parseInt(queryCategoryLevel2));
        }

        if (queryCategoryLevel3 != null && queryCategoryLevel3 != "") {
            appInfo.setCategoryLevel3(Integer.parseInt(queryCategoryLevel3));
        }

        PageUtils pageUtils = new PageUtils();
        if (pageIndex != null && pageIndex != "") {
            pageUtils.setCurrentPage(Integer.parseInt(pageIndex));
        } else {
            pageUtils.setCurrentPage(1);
        }
        pageUtils.setPageSize(5);

        appInfoService.findAll(appInfo, pageUtils);
        model.addAttribute("page", pageUtils);

        List<DataDictionary> flatform = dataDictionaryService.getAllFlatform();
        model.addAttribute("flatform", flatform);

        List<AppCategory> typeLevel1 = appCategoryService.getTypeLevel1();
        model.addAttribute("typeLevel1", typeLevel1);
        return "backend/applist";
    }

    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public String checkPage(Integer aid, Integer vid, Model model) {
        if (aid != null) {
            AppInfo appInfo = new AppInfo();
            appInfo.setId(aid);

            appInfo = appInfoService.getAppInfoById(appInfo);
            model.addAttribute("appInfo", appInfo);
        }

        if (vid != null) {
            AppVersion appVersion = new AppVersion();
            appVersion.setId(vid);
            appVersion = appVersionService.getAppVersionById(appVersion);
            model.addAttribute("appVersion", appVersion);
        }
        return "backend/appcheck";
    }

    @RequestMapping(value = "/updStatus", method = RequestMethod.POST)
    public String updateStatus(AppInfo appInfo) {
        int count = appInfoService.updateStatus(appInfo);
        if (count > 0) {
            return "redirect:/app/queryAppinfo";
        } else {
            return "backend/appcheck";
        }
    }
}
